﻿//∥∥∥∥∥∥∥∥∥∥∥∥∥∥ICode.name 
package ICode.name.net {
	//
	import flash.display.Loader ;
	import flash.display.LoaderInfo ;
	import flash.events.EventDispatcher ;
	import flash.events.Event ;
	import flash.events.IOErrorEvent ;
	import flash.events.ProgressEvent ;
	import flash.net.URLRequest ;
	//
	public class QueLoader extends EventDispatcher {
		//
		public static const  MAX_COUNT:int = 1000 ;
		private var m_arrPath:Array = [] ;
		private var m_arrFunction:Array = [] ;
		private var m_nLength:int = 0 ;
		private var m_obTransfer:EventDispatcher ;
		private var m_arrLoader:Array = [] ;
		private var m_blLastCompleted:Boolean = true ;
		private var m_obLoader:Loader ;
		//
		//∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥构造函数
		public function QueLoader(obTransfer:EventDispatcher = null ){
			super () ;
			m_obTransfer = obTransfer ;
		}
		//∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥队列添加函数
		public function addItem(str:String,obCallBack:Function ):void {
			if(!m_blLastCompleted){
				m_blLastCompleted = true ;
				if(m_obLoader!=null){
					m_obLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE,complete_obLoader );
					m_obLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR ,ioError_obLoader );
					m_obLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS ,progress_obLoader );
				}
				m_arrPath = [] ;
				m_arrFunction = [] ;
			}
			if(m_arrPath!=null && str!=null && m_arrFunction!=null ){
				m_arrPath.push(str );
				m_arrFunction.push(obCallBack ) ;
			}
		}
		//∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥错误处理函数
		private function ioError_obLoader(e:Event):void{ } ;
		//∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥数据加载函数
		public function load():void {
			m_blLastCompleted = false ;
			m_nLength = m_arrPath.length ;
			_load() ;
		}
		//∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥内部加载函数
		private function _load():void {
			if(m_arrPath!=null){
				var str:String = m_arrPath.shift();
				m_obLoader = new Loader();
				m_obLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,complete_obLoader );
				m_obLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR ,ioError_obLoader );
				m_obLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS ,progress_obLoader );
				m_obLoader.load(new URLRequest(str )) ;
			}
		}
		//∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥进度转发函数
		private function progress_obLoader(e:ProgressEvent ):void {
			var percent:int = e.bytesLoaded * 1000 / e.bytesTotal ;
			var obSender:EventDispatcher = this ;
			if(m_obTransfer!=null){
				obSender = m_obTransfer ;
			}
			var nst:int = m_nLength - m_arrPath.length ;
			obSender.dispatchEvent(new ProgressEvent(e.type,e.bubbles,e.cancelable,percent, m_nLength * MAX_COUNT + nst )) ;
		}
		//∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥进度解析函数
		public static function getPercent(value:int,str:String):String {
			var length:int = value /MAX_COUNT ;
			var nst:int = value % length ;
			str = String(nst) + str + length ;
			return str ;
		}
		//∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥加载完成函数
		private function complete_obLoader(e:Event):void {
			if(e!=null && m_arrPath !=null){
				var obInfo:LoaderInfo = e.target as LoaderInfo ;
				obInfo.removeEventListener(Event.COMPLETE,complete_obLoader );
				obInfo.removeEventListener(IOErrorEvent.IO_ERROR ,ioError_obLoader );
				obInfo.removeEventListener(ProgressEvent.PROGRESS ,progress_obLoader );
				var obCallBack:Function = m_arrFunction.shift() ;
				if(obCallBack!=null){
					try{
						obCallBack(obInfo.content ) ;
					}catch(ex:Error){
					}
				}
				if(m_arrPath.length==0){
					m_blLastCompleted = true ;
					dispatchEvent(new Event(Event.COMPLETE )) ;
				}else{
					_load() ;
				}
			}
		}
		//∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥∥
	}
}


